package com.zrrd.jt_admin.shop.category.controller;


import com.zrrd.jt_admin.common.bo.CategoryBo;
import com.zrrd.jt_admin.common.dto.CategorySortDto;
import com.zrrd.jt_admin.common.dto.Sortdata;
import com.zrrd.jt_admin.common.entity.Category;
import com.zrrd.jt_admin.common.response.ResponseResult;
import com.zrrd.jt_admin.shop.category.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wanjl
 * @since 2024-07-04
 */
@Slf4j
@Api(tags = "商品分类管理")
@RestController
@RequestMapping("/admin/category")
@ResponseBody
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    @ApiOperation(value = "拖拽排序",notes = "拖拽排序")
    @PostMapping("/sort")
    public ResponseResult sort(@RequestBody CategorySortDto csd) {
        //log.info("列表"+csd.toString());

        boolean flag =false;
        for (Sortdata sortdata : csd.getSortdata()) {
            flag =categoryService.lambdaUpdate()
                    .eq(Category::getId, sortdata.getId())
                    .set(Category::getOrdered, sortdata.getOrdered())
                    .set(Category::getCategoryId, sortdata.getCategoryId())
                    .update();
        }
        return ResponseResult.updateResponse(flag);
    }

    @ApiOperation(value = "查询列表", notes = "查询列表")
    @GetMapping
    public ResponseResult findAll() {
        List<CategoryBo> sourceList = categoryService.list().stream().map(category -> {
            CategoryBo categoryBo = new CategoryBo();
            BeanUtils.copyProperties(category, categoryBo);
            return categoryBo;
        }).collect(Collectors.toList());
        List<CategoryBo> categoryBoList = iteratorRuleList(sourceList, 0);

        return ResponseResult.queryResponse(categoryBoList);
    }

    private List<CategoryBo> iteratorRuleList(List<CategoryBo> sourceList, Integer cId) {
        List<CategoryBo> newList = new ArrayList<>();
        sourceList.forEach(CategoryBo -> {
            Integer id = CategoryBo.getId();//权限菜单记录的id
            Integer parentId = CategoryBo.getCategoryId();//权限菜单记录的父id
            if (parentId == cId) {
                List<CategoryBo> list = iteratorRuleList(sourceList, id);
                CategoryBo.setChild(list);
                newList.add(CategoryBo);
            }
        });
        return newList;
    }

    @ApiOperation(value = "添加", notes = "添加分类信息")
    @PostMapping
    @ResponseBody
    public ResponseResult add(@RequestBody Category category) {
        return ResponseResult.addResponse(categoryService.save(category));
    }

    @ApiOperation(value = "修改", notes = "修改分类信息")
    @PutMapping
    public ResponseResult update(@RequestBody Category category) {
        return ResponseResult.updateResponse(categoryService.updateById(category));
    }

}

